package com.neu.config;

import cn.dev33.satoken.interceptor.SaRouteInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@EnableWebMvc
@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {
    // 注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册路由拦截器，自定义认证规则
        registry.addInterceptor(new SaRouteInterceptor((req, res, handler) -> {

            // 登录认证 -- 拦截所有路由，并排除/user/login 用于开放登录
//            SaRouter.match("/**", "/user/login", r -> StpUtil.checkLogin());
//            SaRouter.match("/**", "/user/login", r -> StpUtil.checkLogin());

            SaRouter.match("/user/info", r -> StpUtil.checkLogin());
            SaRouter.match("/user/logout", r -> StpUtil.checkLogin());

            // 角色认证
            SaRouter.match("/user/list", r -> StpUtil.checkRoleOr("admin"));
            SaRouter.match("/user/add", r -> StpUtil.checkRoleOr("admin"));
            SaRouter.match("/user/update", r -> StpUtil.checkRoleOr("admin"));
            SaRouter.match("/user/delete", r -> StpUtil.checkRoleOr("admin"));

            SaRouter.match("/article/w", r -> StpUtil.checkRoleOr("admin","editor"));
            SaRouter.match("/material/i", r -> StpUtil.checkRoleOr("admin","editor"));
            SaRouter.match("/material/w", r -> StpUtil.checkRoleOr("admin","editor"));
            SaRouter.match("/material/d", r -> StpUtil.checkRoleOr("admin","editor"));
            SaRouter.match("/project/list", r -> StpUtil.checkRoleOr("admin","editor"));
            SaRouter.match("/project/w", r -> StpUtil.checkRoleOr("admin","editor"));
            SaRouter.match("/project/d", r -> StpUtil.checkRoleOr("admin","editor"));
            SaRouter.match("/project/i", r -> StpUtil.checkRoleOr("admin","editor"));
            SaRouter.match("/project/bi", r -> StpUtil.checkRoleOr("admin","editor"));
            SaRouter.match("/project/plan/w", r -> StpUtil.checkRoleOr("admin","editor"));
            SaRouter.match("/project/plan/i", r -> StpUtil.checkRoleOr("admin","editor"));
            SaRouter.match("/project/plan/d", r -> StpUtil.checkRoleOr("admin","editor"));
            SaRouter.match("/project/plan/bi", r -> StpUtil.checkRoleOr("admin","editor"));
            SaRouter.match("/province/cooperator/i", r -> StpUtil.checkRoleOr("admin","editor"));
            SaRouter.match("/province/cooperator/w", r -> StpUtil.checkRoleOr("admin","editor"));
            SaRouter.match("/province/cooperator/d", r -> StpUtil.checkRoleOr("admin","editor"));
            SaRouter.match("/province/cooperator/bi", r -> StpUtil.checkRoleOr("admin","editor"));

            SaRouter.match("/province/dashboard", r -> StpUtil.checkRoleOr("admin","editor","visitor"));
            SaRouter.match("/province/info", r -> StpUtil.checkRoleOr("admin","editor","visitor"));
            SaRouter.match("/province/cooperator/list", r -> StpUtil.checkRoleOr("admin","editor","visitor"));
            SaRouter.match("/project/plan/list", r -> StpUtil.checkRoleOr("admin","editor","visitor"));
            SaRouter.match("/project/r", r -> StpUtil.checkRoleOr("admin","editor","visitor"));
            SaRouter.match("/article/r", r -> StpUtil.checkRoleOr("admin","editor","visitor"));
            SaRouter.match("/material/list", r -> StpUtil.checkRoleOr("admin","editor","visitor"));

        })).addPathPatterns("/**");
    }
}
